package com.godtree.util.bazhuayu;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

import static com.godtree.util.bazhuayu.TokenTools.getToken;

/**
 * 获取最终任务列表
 *
 * @author Lisicheng
 * @date 2020-6-4
 */
public final class DataTools {

  private static final String URL = "https://dataapi.bazhuayu.com/api/notexportdata/gettop?taskId={}&size={}";

  private static final int SIZE = 100;

  public static List<Map<String, Object>> data(String taskId) {
    JSONObject json = JSONObject.parseObject(
        HttpUtil.createGet(StrUtil.format(URL, taskId, SIZE)).header("Authorization", getToken())
            .execute().body());
    if (Constant.MSG.equals(json.getString(Constant.MSG_ERROR))) {
      return Optional.ofNullable(json.getJSONObject("data")).map(i -> i.getJSONArray("dataList"))
          .orElse(new JSONArray()).stream().map(String::valueOf).map(JSONObject::parseObject)
          .map(JSONObject::getInnerMap).collect(Collectors.toList());
    }
    return Collections.emptyList();
  }

}
